package cn.ljq.controller.tea;

import cn.ljq.dao.*;
import cn.ljq.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RequestMapping("/tea")
public class MyTeaController {

    @Autowired
    UserMapper userMapper;

    @Autowired
    LargeFileMapper largeFileMapper;

    @Autowired
    ClzMapper clzMapper;

    @Autowired
    TaskMapper taskMapper;

    @Autowired
    CourseMapper courseMapper;

    @Autowired
    MarkMapper markMapper;

    @GetMapping("/go2updatePwd")
    public String go2updatePwd(Model model, HttpSession session){

        System.out.println("修改教师密码");



        return "/tea/update";
    }

    @PostMapping("/updatePwd")
    public String updatePwd(Model model,String pwd,HttpSession session){

        System.out.println("从前端拿到的修改密码 == " + pwd);

        Object who = session.getAttribute("DL");  //拿到 session

        User user = (User)who;  //强转成 User 类

        System.out.println("session == " + user);

        String myPhome = user.getPhone();  //从session拿到账号

        User newUser = new User();  //定义空对象

        newUser.setPhone(myPhome);  //把账号存入空对象

        newUser.setPwd(pwd);  //把密码存入空对象

        userMapper.updatePwd(newUser);  //SQL修改密码

        return "forward:/public/logout";

    }


    @GetMapping("/findMyTask")
    public String findMyTask(Model model,HttpSession session){

        User tea = (User)session.getAttribute("DL");  //拿到登录老师的信息

        System.out.println("session里老师的信息==" + tea);

        Integer uid = tea.getUid();  //从session拿到老师的uid

        List<Task> tasks = taskMapper.findByTid(uid);  //找到这个老师在哪些班上什么课

        System.out.println("输出老师所带的所有班级和所教的所有课程==" + tasks);

        model.addAttribute("tasks",tasks);

        return "/tea/myTask";
    }

    @RequestMapping("/findMark")
    public String findMark(Model model,String clzno,String cno){

        Map<String,Object> gomap = new HashMap<>();

        gomap.put("clzno",clzno);  //把班级ID存入map

        gomap.put("cno",cno);  //把课程ID存入map

        List<Mark> marks = new ArrayList<>();  //定义空的分数表集合 用来存下面 for(User s : stus) 查出来的东西

        Clz myClz = clzMapper.findOne(clzno);  //拿到这个班的名字

        Course myCourse = courseMapper.findOne(cno);  //拿到这个课程的名字

        List<User> stus = userMapper.findStuByClz(clzno);  //找到这个班的所有学生

        System.out.println("==========");

        System.out.println(stus);

        System.out.println("=============");

        for (User s : stus){
            System.out.println("-----");
            System.out.println(s);
            System.out.println("-----");
            Mark mark = new Mark();  //定义空的成绩数据
            mark.getClz().setClzno(clzno);  //把班级课程存入假定的Mark
            mark.getClz().setClzname(myClz.getClzname());
            mark.getCourse().setCno(cno);
            mark.getCourse().setCname(myCourse.getCname());

            mark.getStu().setPhone(s.getPhone());   //把电话和姓名存入
            mark.getStu().setUname(s.getUname());

            //先假设这个学生没有成绩
            mark.setId(0);  //假设 0 为没有成绩
            mark.setScore(0.0);  //成绩自然也要定义为 0.0

            Map<String,Object> map = new HashMap<>();
            map.put("phone",s.getPhone());
            map.put("cno",cno);

            Mark k = markMapper.findBySnoCno(map);  //然后再去找这个学生原本有没有成绩

            if (k != null){
                mark.setScore(k.getScore());
                mark.setId(k.getId());
            }

            marks.add(mark);  //这里是添加到了新的集合了

            model.addAttribute("marks",marks);

        }

        model.addAttribute("myClz",myClz);

        model.addAttribute("myCourse",myCourse);

        return "/tea/mark";
    }

    @RequestMapping("/addMark")
    public String addMark(Model model,Mark mark,String clzno,HttpSession session){

        model.addAttribute("action","findMark");

        System.out.println("mark ==+++ " + mark);

        System.out.println("clzno ==+++" + clzno);

        Date date = new Date();  //new 一个日期

        mark.setTpost(date);  //当你登分的时候 可以把日期添加进去

        User teaName = (User) session.getAttribute("DL");  //从session拿到登录老师的信息

        mark.setTid(teaName.getUid());  //谁登的分

        if (mark.getId() == 0){  //如果t_mark表的id是0就添加数据
            System.out.println("------");
            System.out.println(mark);
            markMapper.addMark(mark);
            System.out.println("//////");
            return "forward:/tea/findMark?cno=" + mark.getCourse().getCno();
        }else {  //否则就是原本有分数  那就是修改分数 无需添加
            System.out.println("********");
            markMapper.updateMark(mark);
            System.out.println("+++++++++");
            return "forward:/tea/findMark?cno=" + mark.getCourse().getCno();
        }

    }


}
